package cn.miwu52.homeworkerp.Controller;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import cn.miwu52.homeworkerp.R;
import cn.miwu52.homeworkerp.dao.UserDao;

public class Menu extends AppCompatActivity {
    private int count = 0;
    private ImageView outLogin;
    private ImageView search_button;
    private ImageView entrepotManage;
    private ImageView outEntrepot;
    private ImageView inEntrepot;
    private ImageView information;
    private ImageView userInfo;
    private ImageView addProduct;
    private UserDao userDao;
    private SharedPreferences preferences;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            count++;
            if (count == 2) {
                preferences = getSharedPreferences("MainActivity", Activity.MODE_PRIVATE);
                preferences.edit()
                        .putBoolean("loginStatus", false)
                        .commit();
                System.out.println(keyCode);
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
                System.exit(0);
            }else{
                Toast.makeText(Menu.this,"再点一次退出程序",Toast.LENGTH_SHORT).show();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        outLogin = findViewById(R.id.outLogin);
        search_button = findViewById(R.id.search_button);
        entrepotManage = findViewById(R.id.entrepotManage);
        outEntrepot = findViewById(R.id.outEntrepot);
        inEntrepot = findViewById(R.id.inEntrepot);
        userInfo = findViewById(R.id.userInfo);
        addProduct = findViewById(R.id.addProduct);
        userDao = new UserDao(this);
        preferences = getSharedPreferences("Controller.MainActivity", Activity.MODE_PRIVATE);
        //退出登录
        outLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                preferences.edit()
                        .putBoolean("loginStatus", false)
                        .putBoolean("autoLogin", false)
                        .commit();
                Intent intent = new Intent(Menu.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        //搜索产品
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Menu.this, SearchView.class);
                startActivity(intent);

            }
        });
        //仓库管理
        entrepotManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userDao.findUserGrade(preferences.getString("userName", "userName")).equals("出库")) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(Menu.this, R.raw.quanxianbuzu);
                    mediaPlayer.start();
                    return;
                }
                Intent intent = new Intent(Menu.this, EntrepotView.class);
                startActivity(intent);

            }
        });
        //商品入库
        inEntrepot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userDao.findUserGrade(preferences.getString("userName", "userName")).equals("出库")) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(Menu.this, R.raw.quanxianbuzu);
                    mediaPlayer.start();
                    return;
                }
                Intent intent = new Intent(Menu.this, InEntrepotActivity.class);
                startActivity(intent);

            }
        });
        //商品出库
        outEntrepot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userDao.findUserGrade(preferences.getString("userName", "userName")).equals("入库")) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(Menu.this, R.raw.quanxianbuzu);
                    mediaPlayer.start();
                    return;
                }
                Intent intent = new Intent(Menu.this, OutEntrepotActivity.class);
                startActivity(intent);

            }
        });
        //个人信息
        userInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Menu.this, userInfo.class);
                startActivity(intent);

            }
        });
        //新增商品
        addProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userDao.findUserGrade(preferences.getString("userName", "userName")).equals("出库")) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(Menu.this, R.raw.quanxianbuzu);
                    mediaPlayer.start();
                    return;
                }
                Intent intent = new Intent(Menu.this, addProductActivity.class);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        count = 0;
    }
}